//
//  UserAccount.swift
//  Weibo
//
//  Created by 周先生 on 15/11/11.
//  Copyright © 2015年 周先生. All rights reserved.
//

import UIKit

//遵守NSCoding协议 实现两个方法
class UserAccount: NSObject, NSCoding {

    //用于调用access_token 借口获取授权后的access_token
    var access_token: String?
    //access_token的生命周期  单位是秒数
    var expires_in: NSTimeInterval = 0 {
        didSet{
        
        expires_date = NSDate(timeIntervalSinceNow: expires_in)
            
        }
    }
    //过期日期 开发者是5年 普通用户是三天
    var expires_date: NSDate?
    //当前授权用户的UID
    var uid: String?
    //用户头像地址 String
    var avatar_large: String?
    //用户名
    var name: String?
    
    //KVC设置初始值
    init(dict: [String: AnyObject]) {
        //super 实例化
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    //生写description
    override var description: String {
        
        let keys = ["access_token","expires_date","uid","avatar_large","name","expires_in"]
        return dictionaryWithValuesForKeys(keys).description
        
    }

    //保存对象
    func saveAccount() {
    
        let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
        NSKeyedArchiver.archiveRootObject(self, toFile: path)
        print(path)
    
    }
    //对外提供归档解档方法
    //func是一个类方法 函数前加class
    class func loadAccount() -> UserAccount? {
        //解归档
        //给string对象变成路径的这个方法 swift2.0丢了
        let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
        if let account = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? UserAccount {
            //判断用户是否过期
            if account.expires_date?.compare(NSDate()) == NSComparisonResult.OrderedDescending{
                
                return account
            }
        }
        return nil
    }
    
    //实现NSCoding的协议方法
    //解归档  将磁盘中的二进制数据 -> 装换成自定义对象 跟反序列化很像
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_date = aDecoder.decodeObjectForKey("expires_date") as? NSDate
        uid = aDecoder.decodeObjectForKey("uid") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        name = aDecoder.decodeObjectForKey("name") as? String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")

    }
    //归档  将对象装换成二进制数据 保存到磁盘中 序列化
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expires_date, forKey: "expires_date")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        aCoder.encodeObject(name, forKey: "name")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
    
    }
    }
